/**
 * @Date 2025/1/8 10:20
 * @Created by 衡盛永
 */

package org.example.deploy.controller;

//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.IncorrectCredentialsException;
//import org.apache.shiro.authc.UnknownAccountException;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.subject.Subject;
//import org.example.deploy.dto.ResultDto;
//import org.apache.shiro.SecurityUtils;
import org.example.deploy.utils.TokenUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ShiroLoginController {

//    @PostMapping("login")
//    public ResultDto<HashMap<String, Object>> login(@RequestBody Map<String, Object> userInfo) {
    // 1. 创建用户 Token（输入凭证）
//        UsernamePasswordToken token = new UsernamePasswordToken((String) userInfo.get("username"), (String) userInfo.get("password"));
//        try {
//            // 2. 执行登录操作
//            Subject currentUser = SecurityUtils.getSubject();
//            currentUser.login(token); // 调用 Realm 验证
//            HashMap<String, Object> userMap = new HashMap<>();
//            userMap.put("username", userInfo.get("username"));
//            HashMap<String, Object> responseMap = new HashMap<>();
//            String signToken = TokenUtil.sign(userMap);
//            responseMap.put("token", signToken);
//            responseMap.put("username", userInfo.get("username"));
//            return ResultDto.success("登录成功", responseMap);
//        } catch (UnknownAccountException uae) {
//            return ResultDto.error(201, "用户名不存在！");
//        } catch (IncorrectCredentialsException ice) {
//            return ResultDto.error(201, "密码错误！");
//        } catch (AuthenticationException ae) {
//            return ResultDto.error(201, "认证失败！");
//        }
//    }
}
